package app.recycler.com.recycler.contacts;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.google.gson.Gson;

import java.util.ArrayList;
import java.util.List;

import app.recycler.com.recycler.MainActivity;
import app.recycler.com.recycler.R;
import app.recycler.com.recycler.ui.ContactsActivity;

import static android.app.Activity.RESULT_OK;

public class ContactRVAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private List<UploadContactBean> contactList = new ArrayList<>();

    public ContactRVAdapter(List<UploadContactBean> unlockADataLis) {
        String first = "";
        for (UploadContactBean unlockADataLi : unlockADataLis) {
            String firstLetter = unlockADataLi.firstLetter;
            if (!first.endsWith(firstLetter)) {
                UploadContactBean bean = new UploadContactBean();
                bean.isIncise = true;
                bean.firstLetter = firstLetter;
                first = firstLetter;
                contactList.add(bean);
            }
            contactList.add(unlockADataLi);
        }
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        if (viewType == 1) {
            View view =
                    LayoutInflater.from(parent.getContext()).inflate(R.layout.recycler_item_contact_title_item, parent, false);
            return new TitleHolder(view);
        } else {
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.recycler_item_contact_name_item, parent, false);
            return new NameHolder(view);
        }
    }

    @Override
    public void onBindViewHolder(@NonNull final RecyclerView.ViewHolder holder, final int position) {
        if (holder instanceof TitleHolder) {
            ((TitleHolder) holder).mTitle.setText(contactList.get(position).firstLetter);
        } else if (holder instanceof NameHolder) {
            ((NameHolder) holder).mName.setText(contactList.get(position).name);
            ((NameHolder) holder).mPhoneNumber.setText(contactList.get(position).phoneNumber);
            ((NameHolder) holder).itemView.setTag(contactList.get(position));
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Context context = v.getContext();
                    Object obj = holder.itemView.getTag();
                    if (context instanceof Activity && obj instanceof UploadContactBean) {
                        Intent intent = new Intent();
                        ReturnContracts returnContacts = new ReturnContracts();
                        Intent arg = ((Activity) context).getIntent();
                        returnContacts.tag = arg.getStringExtra(ContactsActivity.KEY_CONTACTS_TAG);
                        returnContacts.name = ((UploadContactBean) obj).name;
                        returnContacts.num = ((UploadContactBean) obj).phoneNumber;
                        intent.putExtra(MainActivity.RESULT_CONTRACTS, new Gson().toJson(returnContacts));
                        ((Activity) context).setResult(RESULT_OK, intent);
                        ((Activity) context).finish();
                    }
                }
            });
        }
    }

    @Override
    public int getItemViewType(int position) {
        boolean isIncise = contactList.get(position).isIncise;
        if (isIncise) {
            return 1;
        } else {
            return 2;
        }
    }

    @Override
    public int getItemCount() {
        return contactList.size();
    }

    public static class TitleHolder extends RecyclerView.ViewHolder {
        TextView mTitle;

        public TitleHolder(View itemView) {
            super(itemView);
            mTitle = itemView.findViewById(R.id.title);
        }
    }

    public static class NameHolder extends RecyclerView.ViewHolder {
        TextView mName;
        TextView mPhoneNumber;

        public NameHolder(View itemView) {
            super(itemView);
            mName = itemView.findViewById(R.id.name);
            mPhoneNumber = itemView.findViewById(R.id.phone_number);
        }
    }
}
